package com.atguigui.leetcode;


/**
 * 652.寻找重复的子树
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by WJX on 2022/9/5 9:10
 */
public class P652FindDuplicateSubtrees {
    public static void main(String[] args) {
        Solution solution = new P652FindDuplicateSubtrees().new Solution();
        // TO TEST
    }

    class Solution {

        /*Map<String, Pair<TreeNode, Integer>> seen = new HashMap<String, Pair<TreeNode, Integer>>();
        Set<TreeNode> repeat = new HashSet<TreeNode>();
        int idx = 0;

        public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
            dfs(root);
            return new ArrayList<TreeNode>(repeat);
        }

        private int dfs(TreeNode node) {
            if (node == null) {
                return 0;
            }
            //三元数组结构
            *//*
                根节点的值为 xx；
                左子树的序号为 ll；
                右子树的序号为 rr
             *//*
            int[] tri = {node.val, dfs(node.left), dfs(node.right)};
            String hash = Arrays.toString(tri);
            if (seen.containsKey(hash)) {
                Pair<TreeNode, Integer> pair = seen.get(hash);
                repeat.add(pair.getKey());
                return pair.getValue();
            } else {
                seen.put(hash, new Pair<TreeNode, Integer>(node, ++idx));
                return idx;
            }
        }*/
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

}
